<template>
  <div>
    <el-dialog
      title="修改分类"
      :visible.sync="dialogVisible"
      width="25%"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="顶级分类" prop="pid">
          <el-select v-model="form.pid" placeholder="如果为空，默认为顶级菜单" style="width: 250px">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="分类图片" prop="img" v-if="form.pid===0">
          <el-upload
            class="avatar-uploader"
            action="http://admin.api.highsuzhou.tech/upload/uploadImages"
            :show-file-list="false"
            :on-success="handleOk"
          >
            <img v-if="form.img" :src="form.img" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="handleAdd">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
  import {classifyAdd, findById, pList} from "@/api/classify";

  export default {
    name: "create",
    data() {
      return {
        dialogVisible: false,
        form: {
          id: '',
          name: '',
          pid: '',
          img: ''
        },
        options: [],
        rules: {
          name: [
            {required: true, message: '请输入分类名称', trigger: 'blur'},
          ],
        }
      }
    },
    methods: {
      show(id) {
        pList().then(res => {
          this.options = res.result
        })
        findById(id).then(res => {
          this.form.id = res.result.id
          this.form.pid = res.result.pid
          this.form.name = res.result.name
          this.form.img = res.result.img
        })
        this.dialogVisible = true
      },
      handleAdd() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            classifyAdd(this.form).then(res => {
              if (res.status === 200) {
                this.$message.success("修改成功")
                this.dialogVisible = false
                this.$emit('Ok')
              }
            })
          } else {
            return false;
          }
        });
      },
      handleOk(file) {
        this.form.img = file.result
      }
    }
  }
</script>

<style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
